import 'package:flutter/material.dart';
import 'package:ct_pa_majordomo/pages/mine/online_pay/state/online_pay_state.dart';
import 'package:ct_pa_majordomo/config/theme_vars.dart';
import 'package:ct_pa_majordomo/widgets/normal_scaffold/index.dart';

class BasicInfoPage extends StatefulWidget {
  final OnlinePayState state;
  final VoidCallback onStateChanged;
  final bool isEnterprise;
  final bool isReadOnly;

  const BasicInfoPage({
    super.key,
    required this.state,
    required this.onStateChanged,
    required this.isEnterprise,
    this.isReadOnly = false,
  });

  @override
  State<BasicInfoPage> createState() => _BasicInfoPageState();
}

class _BasicInfoPageState extends State<BasicInfoPage> {
  final _formKey = GlobalKey<FormState>();
  late TextEditingController _accountNameController;
  late TextEditingController _contactPhoneController;
  late TextEditingController _contactEmailController;

  @override
  void initState() {
    super.initState();
    // 根据类型初始化控制器
    if (widget.isEnterprise) {
      _accountNameController = TextEditingController(text: widget.state.company["accountName"]);
      _contactPhoneController = TextEditingController(text: widget.state.company["contactPhone"]);
      _contactEmailController = TextEditingController(text: widget.state.company["contactEmail"]);
    } else {
      _accountNameController = TextEditingController(text: widget.state.person["accountName"]);
      _contactPhoneController = TextEditingController(text: widget.state.person["contactPhone"]);
      _contactEmailController = TextEditingController(text: widget.state.person["contactEmail"]);
    }
  }

  @override
  void dispose() {
    _accountNameController.dispose();
    _contactPhoneController.dispose();
    _contactEmailController.dispose();
    super.dispose();
  }

  void _saveForm() {
    if (_formKey.currentState?.validate() ?? false) {
      if (widget.isEnterprise) {
        widget.state.updateCompanyBasicInfo(
          accountName: _accountNameController.text,
          contactPhone: _contactPhoneController.text,
          contactEmail: _contactEmailController.text,
        );
      } else {
        widget.state.updatePersonBasicInfo(
          accountName: _accountNameController.text,
          contactPhone: _contactPhoneController.text,
          contactEmail: _contactEmailController.text,
        );
      }
      widget.onStateChanged();
      Navigator.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "基本信息",
      child: Form(
        key: _formKey,
        child: ListView(
          padding: const EdgeInsets.all(ThemeVars.paddingPage),
          children: [
            if (widget.isReadOnly) _buildReadOnlyTip(),
            TextFormField(
              controller: _accountNameController,
              enabled: !widget.isReadOnly,
              decoration: InputDecoration(
                labelText: '商户简称',
                hintText: '请输入商户简称',
              ),
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return '请输入商户简称';
                }
                return null;
              },
            ),
            const SizedBox(height: 16),
            TextFormField(
              controller: _contactPhoneController,
              enabled: !widget.isReadOnly,
              decoration: const InputDecoration(
                labelText: '联系手机',
                hintText: '请输入联系手机',
              ),
              keyboardType: TextInputType.phone,
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return '请输入联系手机';
                }
                if (!RegExp(r'^1[3-9]\d{9}$').hasMatch(value)) {
                  return '请输入正确的手机号码';
                }
                return null;
              },
            ),
            const SizedBox(height: 16),
            TextFormField(
              controller: _contactEmailController,
              enabled: !widget.isReadOnly,
              decoration: const InputDecoration(
                labelText: '联系邮箱',
                hintText: '请输入联系邮箱',
              ),
              keyboardType: TextInputType.emailAddress,
              validator: (value) {
                if (value == null || value.isEmpty) {
                  return '请输入联系邮箱';
                }
                if (!RegExp(r'^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$').hasMatch(value)) {
                  return '请输入正确的邮箱地址';
                }
                return null;
              },
            ),
            const SizedBox(height: 32),
            if (!widget.isReadOnly)
              SizedBox(
                width: double.infinity,
                height: 44,
                child: ElevatedButton(
                  onPressed: _saveForm,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: ThemeVars.colorPrimary,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  child: const Text(
                    '保存',
                    style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
  
  Widget _buildReadOnlyTip() {
    return Container(
      margin: const EdgeInsets.only(bottom: 16),
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.blue.withOpacity(0.1),
        borderRadius: BorderRadius.circular(8),
        border: Border.all(color: Colors.blue.withOpacity(0.3)),
      ),
      child: Row(
        children: [
          Icon(
            Icons.info_outline,
            color: Colors.blue,
            size: 20,
          ),
          const SizedBox(width: 8),
          Expanded(
            child: Text(
              '当前信息为只读状态，无法修改',
              style: TextStyle(
                color: Colors.blue.shade700,
                fontSize: 14,
              ),
            ),
          ),
        ],
      ),
    );
  }
} 